package com.onlygx.dva.common.exception;


import com.onlygx.dva.common.utils.RequestLogUtil;
import com.onlygx.dva.common.entity.common.RT;
import com.onlygx.dva.common.properties.CommonCoreProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 * 作者: GaoXiang
 * 时间: 2021/12/16
 */
@Slf4j
@ControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

    final CommonCoreProperties coreProperties;

    @ExceptionHandler(value = FailRequestException.class)
    @ResponseBody
    public RT<Void> failRequestException(HttpServletRequest request, FailRequestException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(501, "访问失败:"+ex.getMessage());
    }

    @ExceptionHandler(value = NumberFormatException.class)
    @ResponseBody
    public RT<Void> numberFormatException(HttpServletRequest request,NumberFormatException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(431, "类型转换失败:"+ex.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public RT<Void> typeMismatchException(HttpServletRequest request,MethodArgumentTypeMismatchException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(432, "请求参数格式不支持:"+ex.getMessage());
    }

    @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public RT<Void> exceptionHandler(HttpServletRequest request,HttpMediaTypeNotSupportedException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(433, "请求内容类型不支持:"+ex.getMessage());
    }

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public RT<Void> exceptionHandler(HttpServletRequest request,HttpMessageNotReadableException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(434, "解析参数失败:"+ex.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public RT<Void> validExceptionHandler(HttpServletRequest request,MethodArgumentNotValidException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(435, "参数验证失败:"+ex.getMessage());
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public RT<Void> methodExceptionHandler(HttpServletRequest request,HttpRequestMethodNotSupportedException ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(436, "请求方式不支持:"+ex.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public RT<Void> nullPointerExceptionHandler(HttpServletRequest request, Exception ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(437, "传参异常，处理失败。" + ex.getMessage());
    }

    @ExceptionHandler(value = NotFindResultException.class)
    @ResponseBody
    public RT<Void> notFindResultException(HttpServletRequest request, Exception ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(451, ex.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RT<Void> exceptionHandler(HttpServletRequest request, Exception ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(500, ex.getMessage());
    }


    @ExceptionHandler(value = AuthorizeCodeInValidException.class)
    @ResponseBody
    public RT<Void> AuthorizeCodeInValidExceptionHandler(HttpServletRequest request, Exception ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(4114, ex.getMessage());
    }

    @ExceptionHandler(value = AuthorizeCodeNotExistException.class)
    @ResponseBody
    public RT<Void> AuthorizeCodeNotExistExceptionHandler(HttpServletRequest request, Exception ex) {
        RequestLogUtil.printRequestErrorInfo(log,request, ex);
        if(coreProperties.getShowExceptionStackTrace() == 1){
            ex.printStackTrace();
        }
        return RT.error(4113, ex.getMessage());
    }

}
